package com.yingpai.fitness.dvp_2_0.main.activity.fitness;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.adapter.BaseAdapterHelper;
import com.yingpai.fitness.dvp_2_0.common.adapter.QuickAdapter;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.common.view.XListView;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.entity.store.PersonalSelectBean;
import com.zzhoujay.richtext.RichText;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ASUS on 2018/8/14.
 */

public class PersonalSelectActivity extends CommonActivity implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.richTv)
    TextView richTv;
    @BindView(R.id.listview)
    XListView listview;
    @BindView(R.id.swiperefresh)
    SwipeRefreshLayout swiperefresh;
    private int storeId;
    private int dicCurriculumCategoryId;
    private QuickAdapter<PersonalSelectBean.MapData.DataData> quickAdapter;
    private DecimalFormat df = new DecimalFormat("######.###");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.activity_personal_select);
    }

    @Override
    protected void initView() {
        setTitle("私教选择");
        swiperefresh.setOnRefreshListener(this);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            storeId = bundle.getInt("storeId", -1);
            dicCurriculumCategoryId = bundle.getInt("dicCurriculumCategoryId", -1);
        }

        quickAdapter = new QuickAdapter<PersonalSelectBean.MapData.DataData>(this, R.layout.item_personal) {
            @Override
            protected void convert(BaseAdapterHelper helper, PersonalSelectBean.MapData.DataData item) {
                helper.setText(R.id.item_name, item.getCoach_name());
                helper.setText(R.id.money, "￥" + df.format(item.getPrice() / 100));
                ImageView photo = helper.getView(R.id.photo);
                Glide.with(context)
//                        .load(item.getPhoto_urls().substring(0, item.getPhoto_urls().indexOf(",")))
                        .load(item.getCoach_photo_url())
                        .error(R.mipmap.default_big_bg)
                        .into(photo);
            }
        };

        listview.setAdapter(quickAdapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle1 = new Bundle();
                bundle1.putInt("storeId", storeId);
                bundle1.putInt("courseId", quickAdapter.getItem(position).getId());
                startActivity(PersonalDetailActivity.class, bundle1);
            }
        });
    }

    @Override
    protected void initData() {
        requestData();
    }


    private void requestData() {
        showDialog(this, null);
        HttpParams params = new HttpParams();
        params.put("storeId", storeId);
        params.put("dicCurriculumCategoryId", dicCurriculumCategoryId);
        params.put("type", 2);
        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.PERSONAL_SELECT, params, Url.PERSONAL_SELECT, new Callback<PersonalSelectBean>() {
            @Override
            public void onSuccess(PersonalSelectBean personalSelectBean) {
                if (Constant.REQUEST_SUCCESS.equals(personalSelectBean.getResult())) {
                    if (personalSelectBean.getMap().getData() != null && personalSelectBean.getMap().getData().size() != 0) {
                        empty.setVisibility(View.GONE);
                    } else {
                        empty.setVisibility(View.VISIBLE);
                    }
                    RichText.fromHtml(personalSelectBean.getMap().getInfo() == null ? "" : personalSelectBean.getMap().getInfo()).into(richTv);
                    quickAdapter.clear();
                    quickAdapter.addAll(personalSelectBean.getMap().getData());
                    quickAdapter.notifyDataSetChanged();
                } else {
                    empty.setVisibility(View.VISIBLE);
                    showToast(personalSelectBean.getMsg());
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                empty.setVisibility(View.VISIBLE);
                dismissDialog();
            }
        });
    }

    @Override
    public void onRefresh() {
        requestData();
    }

    @Override
    @OnClick({})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {

        }
    }
}
